package com.ph.shopping.common.util.http;

import javax.servlet.http.HttpServletRequest;

/**
 * @项目：phshopping-common-util
 *
 * @描述：获取远程请求IP地址
 *
 * @作者： Mr.chang
 *
 * @创建时间：2017年4月7日
 *
 * @Copyright @2017 by Mr.chang
 */
public class IPUtil {
	
   /**
	 * 获取请求的真实IP地址(适用于公网和局域网)
	 * @param request
	 * @return
	 * @author Mr.Chang
	 * 
	 */
	public static String getIpAddress(HttpServletRequest request) { 
		    String ip = request.getHeader("x-forwarded-for"); 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("Proxy-Client-IP"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("WL-Proxy-Client-IP"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("HTTP_CLIENT_IP"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getRemoteAddr(); 
		    } 
		    return ip; 
		  } 
}
